package com.dc.commons.shiro.credential;

import java.io.Serializable;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.lang3.time.DateUtils;

public class ExcessiveAttempts implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * 一级计数器
	 */
	private AtomicInteger firstCount=new AtomicInteger(0);
	/**
	 * 一级失效时间
	 */
	private Date firstExcessiveDate;
	/**
	 * 最大登陆次数
	 */
	private int firstMaxCount=3;
	
	/**
	 * 一级有效期
	 */
	private int firstValidityPeriod=5;
	
	
	public boolean valid(){
		
			// 如果有效时间没有，或者已经过了有效期 直接将计数器清零。
		if(firstExcessiveDate!=null&&firstExcessiveDate.before(new Date())){
			firstCount=new AtomicInteger(0);
			firstExcessiveDate=null;
		}
		if(firstCount.incrementAndGet()>firstMaxCount){
			firstExcessiveDate=DateUtils.addMinutes(new Date(), firstValidityPeriod);
			return false;
		}
		return true;
	}
	
	
}
